package com.sun.xacml.attr;

import java.net.URI;
import java.util.List;

import org.jdom.Namespace;
import org.jdom.input.DOMBuilder;
import org.w3c.dom.Element;

import com.vividsolutions.jts.geom.Geometry;

public class GeometryAttribute extends AttributeValue {
	
	public static final String identifier = "urn:ogc:def:dataType:geoxacml:1.0:geometry";
	private static URI identifierURI;
	private Geometry geometry;
	private String srsName; 
	private String value;
	/**
     * RuntimeException that wraps an Exception thrown during the
     * creation of identifierURI, null if none.
     */
    private static RuntimeException earlyException;
	
	/**
     * Static initializer that initializes the identifierURI
     * class field so that we can catch any exceptions thrown
     * by URI(String) and transform them into a RuntimeException.
     * Such exceptions should never happen but should be reported
     * properly if they ever do.
     */
    static {
        try {
            identifierURI = new URI(identifier);
        } catch (Exception e) {
            earlyException = new IllegalArgumentException();
            earlyException.initCause(e);
        }
    };
	
	public GeometryAttribute(){
		super(identifierURI);		
	}

	/** Constructor DOMROOT */
	public GeometryAttribute(Element domRoot){
		super(identifierURI);
		
		//Chuyen DOM ve JDOM
		DOMBuilder domBuilder = new DOMBuilder();
		org.jdom.Element jdomRoot = domBuilder.build(domRoot);
		
		Namespace gml = null;
		List additionalNamespaces = jdomRoot.getAdditionalNamespaces();
		for (int ix = 0; ix < additionalNamespaces.size(); ix++)
			if ("gml".equals(((Namespace) additionalNamespaces.get(ix))
					.getPrefix()))
				gml = (Namespace) additionalNamespaces.get(ix);
		
		//Kiem tra loai cua geometry
		/* Point */
		if(jdomRoot.getChild("Point", gml) != null){
			PointAttribute point = new PointAttribute(jdomRoot.getChild("Point", gml));
			geometry = point.getGeometry();
			srsName = point.getSrsName();
			value = point.encode();
		}
		/* Polygon */
		else if(jdomRoot.getChild("Polygon", gml) != null){
			PolygonAttribute polygon = new PolygonAttribute(jdomRoot.getChild("Polygon", gml));
			geometry = (new PolygonAttribute(jdomRoot.getChild("Polygon", gml))).getGeometry();
			srsName = polygon.getSrsName();
			value = polygon.encode();
			
		}
		/* LineString */
		else if(jdomRoot.getChild("LineString", gml) != null){
			LineStringAttribute linestring = new  LineStringAttribute(jdomRoot.getChild("LineString", gml));
			geometry = linestring.getGeometry();
			srsName = linestring.getSrsName();
			value = linestring.encode();
		}
		/* LinearRing */
		else if(jdomRoot.getChild("LinearRing", gml) != null){
			LinearRingAttribute linearring = new LinearRingAttribute(jdomRoot.getChild("LinearRing", gml));
			geometry = linearring.getGeometry();
			srsName = linearring.getSrsName();
			value = linearring.encode();
		}
		/* Box */
		else if(jdomRoot.getChild("Box", gml) != null){
			BoxAttribute box = new BoxAttribute(jdomRoot.getChild("Box", gml));
			srsName = box.getSrsName();
			value = box.encode();
		}
	}
	
	/** Constructor jdomRoot */
	public GeometryAttribute(org.jdom.Element jdomRoot){
		super(identifierURI);
		
		Namespace gml = null;
		List additionalNamespaces = jdomRoot.getAdditionalNamespaces();
		for (int ix = 0; ix < additionalNamespaces.size(); ix++)
			if ("gml".equals(((Namespace) additionalNamespaces.get(ix))
					.getPrefix()))
				gml = (Namespace) additionalNamespaces.get(ix);
		
		//Kiem tra loai cua geometry
		/* Point */
		if(jdomRoot.getChild("Point", gml) != null){
			PointAttribute point = new PointAttribute(jdomRoot.getChild("Point", gml));
			geometry = point.getGeometry();
			srsName = point.getSrsName();
			value = point.encode();
		}
		/* Polygon */
		else if(jdomRoot.getChild("Polygon", gml) != null){
			PolygonAttribute polygon = new PolygonAttribute(jdomRoot.getChild("Polygon", gml));
			geometry = (new PolygonAttribute(jdomRoot.getChild("Polygon", gml))).getGeometry();
			srsName = polygon.getSrsName();
			value = polygon.encode();
		}
		/* LineString */
		else if(jdomRoot.getChild("LineString", gml) != null){
			LineStringAttribute linestring = new  LineStringAttribute(jdomRoot.getChild("LineString", gml));
			geometry = linestring.getGeometry();
			srsName = linestring.getSrsName();
			value = linestring.encode();
		}
		/* LinearRing */
		else if(jdomRoot.getChild("LinearRing", gml) != null){
			LinearRingAttribute linearring = new LinearRingAttribute(jdomRoot.getChild("LinearRing", gml));
			geometry = linearring.getGeometry();
			srsName = linearring.getSrsName();
			value = linearring.encode();
		}
		/* Box */
		else if(jdomRoot.getChild("Box", gml) != null){
			BoxAttribute box = new BoxAttribute(jdomRoot.getChild("Box", gml));
			srsName = box.getSrsName();
			value = box.encode();
		}
	}

	//get geometry
	public Geometry getGeometry() {
		return geometry;
	}
	
	//get srsName
	public String getSrsName(){
		return srsName;
	}
	
	protected GeometryAttribute(URI type) {
		super(type);
		// TODO Auto-generated constructor stub
	}

	@Override
	public String encode() {
		// TODO Auto-generated method stub
		return value;
	}

	public static AttributeValue getInstance(Element root) {
		// TODO Auto-generated method stub
		return new GeometryAttribute(root);
	}

	public static AttributeValue getInstance(String value) {
		// TODO Auto-generated method stub
		return null;
	}
	
	 /**
     * Returns true if the input is an instance of this class and if its
     * value equals the value contained in this class.
     *
     * @param geo the object to compare
     *
     * @return true if this object and the input represent the same value
     */
    public boolean equals(GeometryAttribute geo) {
    	
        return this.geometry.equals(geo.geometry);
    }

}
